/**
 * Created by xiangyufang on 2016/9/8.
 */
;(function($,window,document,undefined){
    // 获取动态表单提交过来的数据
    var postData = {
        deviceId: function(){
            return $('#JQ_deviceId').val();
        }(),
        eqptType: function(){
            return $('#JQ_eqptType').val();
        }()
    };

    DyyBase.ajax(corrctrlApi.url_device_pile(),{eqptId:postData.deviceId},function(dataSet){
        if(dataSet.bool){
            $('#JQ_project_pile').text(dataSet.result.list[0].pipeLineName);
            $('#JQ_project_name').text(dataSet.result.list[0].customNumber);
            $('#JQ_project_type').text(dataSet.result.list[0].solderJointsCorrosion);
            $('#JQ_project_function').text(dataSet.result.list[0].purpose);
        }
    });


    DyyBase.ajax(corrctrlApi.url_record_power_chart(), postData, function (dataSet) {
        if(dataSet.bool){
            //阴保测试桩折线图
            var pileLineData = dataSet.result.cptsPotentialCount;
            if(pileLineData){
                var minData = [];
                for (var i=0;i<pileLineData.length;i++){
                    if(pileLineData[i].ONPOTENTIAL){
                        var x= parseInt(pileLineData[i].ONPOTENTIAL);
                        minData.push(x);
                    }
                    if(pileLineData[i].OFFPOTENTIAL){
                        var y= parseInt(pileLineData[i].OFFPOTENTIAL);
                        minData.push(y);
                    }
                    if(pileLineData[i].FREECORROSIONPOTENTIAL){
                        var z= parseInt(pileLineData[i].FREECORROSIONPOTENTIAL);
                        minData.push(z);
                    }

                }
                var minVal = d3.min(minData);
                var areaMax;
                var areaMin;
                var areaText;
                if(minVal>-850){
                    areaMax = 0;
                    areaMin = 0;
                    areaText = ''
                }else if(minVal<-1200){
                    areaMax = -850;
                    areaMin = -1200;
                    areaText = '保护区间';
                }else{
                    areaMax = -850;
                    areaMin = minVal;
                    areaText = ''
                }

                //通电电位
                var onavg = d3Charts.formart(pileLineData,{x:'RECORDTIME',y:'ONPOTENTIAL'});

                // 断电电位
                var offavg = d3Charts.formart(pileLineData,{x:'RECORDTIME',y:'OFFPOTENTIAL'});

                // 自然电位
                var freeavg = d3Charts.formart(pileLineData,{x:'RECORDTIME',y:'FREECORROSIONPOTENTIAL'});

                // 电位
                var electric = [onavg, offavg, freeavg];
                // 电位图表
                if(electric[0].length>0 || electric[1].length>0 || electric[2].length>0){
                    d3Charts.ordinal({
                        container: 'JQ_pile_electric_chart',
                        color: ['#27be5f', '#fc633a', '#479ee3'],
                        data: electric,
                        tip: {
                            show: true,
                            xtag: '记录时间：',
                            ytag: ['通电电位(mV)：','断点电位(mV)：','自然电位(mV)：']
                        },
                        xAxis: {                         // x轴数据
                            data: [],
                            ticks: 0
                        },
                        yAxis: {                         // x轴数据
                            data: [],
                            ticks: 10
                        },
                        layout: {
                            xtag: '',
                            ytag: '电位值(mV)',
                            margin: {
                                left: 50,
                                right: 50,
                                top: 30,
                                bottom: 40
                            },
                            xgrid: false,
                            ygrid: true
                        },
                        pointText: {
                            show: false
                        },
                        areaShow: true,
                        area: [
                            {
                                range: [areaMin,areaMax],
                                background: '#edeffc',
                                text: areaText
                            }
                        ]
                    });
                }else{
                    $('#JQ_pile_electric_chart').trigger('clear');
                }
            }
        }
    });

    //阴保电源图表导出
    $('#JQ_pile_chart_export').click(function(){
        var pipeName = '阴保测试桩';
        var projectName = $('#JQ_project_name').text();
        $(this).EXPT({
            type: 'name',
            name: pipeName + projectName + '运行统计图表',
            yesBtnAct: function(callback,fname){
                callback();
                canvasPdf.exportHtmlPdf({
                    dom: $('#JQ_chart_pile_div'),
                    server: DyyBase.root_path + '/comm/downImgPdf.json',
                    param: {
                        fileName: fname,
                        name: projectName + '运行统计图表'
                    }
                });
            }
        });
    });
})(jQuery,window,document);